package com.example.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.plan.common.result.Result;
import com.example.plan.exception.BusinessException;
import com.example.plan.mapper.ArNavigationMapper;
import com.example.plan.model.constnant.DroneEnums;
import com.example.plan.model.dto.arNavigation.ArNavigationDTO;
import com.example.plan.model.dto.arNavigation.ArNavigationPageDTO;
import com.example.plan.model.entity.ArNavigation;
import com.example.plan.model.vo.ArNavigationVO;
import com.example.plan.service.ArNavigationService;
import com.example.plan.utils.BeanPageUtil;
import org.springframework.stereotype.Service;

import static com.example.plan.exception.ErrorCode.AR_NAVIGATION_NOT_FOUND;
import static com.example.plan.exception.ErrorCode.UPDATE_AR_NAVIGATION_ID_IS_NULL;

/**
* @author ASUS
* @description 针对表【ar_navigation(AR导航标记表)】的数据库操作Service实现
* @createDate 2025-03-01 19:07:17
*/
@Service
public class ArNavigationServiceImpl extends ServiceImpl<ArNavigationMapper, ArNavigation> implements ArNavigationService{

    /**
     * 保存AR导航标记
     * @param dto
     * @return
     */
    @Override
    public Result<Void> saveArNavigation(ArNavigationDTO dto) {
        ArNavigation arNavigation = BeanUtil.copyProperties(dto, ArNavigation.class);
        return baseMapper.insert(arNavigation)>0?Result.success("保存成功"):Result.failed("保存失败");
    }

    /**
     * 删除AR导航标记
     * @param navId
     * @return
     */
    @Override
    public Result<Void> deleteArNavigation(Long navId) {
        boolean flag=baseMapper.selectById(navId)==null||baseMapper.deleteById(navId)>0;
        return flag?Result.success("删除成功"):Result.failed("删除失败");
    }

    /**
     * 查询AR导航标记详情
     * @param navId
     * @return
     */
    @Override
    public Result<ArNavigationVO> getArNavigation(Long navId) {
        ArNavigation arNavigation = baseMapper.selectById(navId);
        if (arNavigation==null){
            throw new BusinessException(AR_NAVIGATION_NOT_FOUND);
        }
        ArNavigationVO arNavigationVO = BeanUtil.copyProperties(arNavigation, ArNavigationVO.class);
        return Result.success("AR导航标记详情",arNavigationVO);
    }

    /**
     * 分页带条件查询AR导航标记
     * @param dto
     * @return
     */
    @Override
    public Result<IPage<ArNavigationVO>> pageArNavigation(ArNavigationPageDTO dto) {
        QueryWrapper<ArNavigation> queryWrapper=new QueryWrapper<>();
        if (dto.getSortOrder()== DroneEnums.sortOrderEnum.ASC.getCode()){
            queryWrapper.orderByAsc(StrUtil.isNotBlank(dto.getSortField()),dto.getSortField());
        }else {
            queryWrapper.orderByDesc(StrUtil.isNotBlank(dto.getSortField()),dto.getSortField());
        }
        LambdaQueryWrapper<ArNavigation> lambdaQueryWrapper=queryWrapper
                .lambda()
                .eq(dto.getMarker_type()!=null,ArNavigation::getMarker_type,dto.getMarker_type())
                .eq(dto.getMarker_coords()!=null,ArNavigation::getMarker_coords,dto.getMarker_coords())
                .eq(dto.getActivated_at()!=null,ArNavigation::getActivated_at,dto.getActivated_at());
        Page<ArNavigation> page = new Page<>(dto.getCurrent(), dto.getPageSize());
        Page<ArNavigation> ArNavigation = baseMapper.selectPage(page, lambdaQueryWrapper);
        Page<ArNavigationVO> pagevo = BeanPageUtil.copyBean(ArNavigation, ArNavigationVO.class);
        return Result.success(pagevo);
    }

    /**
     * 修改AR导航标记
     * @param dto
     * @return
     */
    @Override
    public Result<Void> updateArNavigation(ArNavigationDTO dto) {
        if (dto.getNav_id()==null){
            throw new BusinessException(UPDATE_AR_NAVIGATION_ID_IS_NULL);
        }
        ArNavigation arNavigation = BeanUtil.copyProperties(dto, ArNavigation.class);
        return baseMapper.updateById(arNavigation)>0?Result.success("修改成功"):Result.failed("修改失败");
    }

}




